# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
#
# secfixes:
#   2.35.2-r0:
#     - CVE-2022-24765
#   2.30.2-r0:
#     - CVE-2021-21300
#   2.26.2-r0:
#     - CVE-2020-11008
#   2.26.1-r0:
#     - CVE-2020-5260
#   2.24.1-r0:
#     - CVE-2019-1348
#     - CVE-2019-1349
#     - CVE-2019-1350
#     - CVE-2019-1351
#     - CVE-2019-1352
#     - CVE-2019-1353
#     - CVE-2019-1354
#     - CVE-2019-1387
#     - CVE-2019-19604
#   2.19.1-r0:
#     - CVE-2018-17456
#   2.17.1-r0:
#     - CVE-2018-11233
#     - CVE-2018-11235
#   2.14.1-r0:
#     - CVE-2017-1000117
#   0:
#     - CVE-2021-29468
#     - CVE-2021-46101

pkgname=git
pkgver=2.37.0
pkgrel=0
pkgdesc="Distributed version control system"
url="https://www.git-scm.com/"
arch="all"
license="GPL-2.0-or-later"
# we need tcl and tk to be built before git due to git-gui and gitk
makedepends="zlib-dev openssl1.1-compat-dev curl-dev expat-dev perl-dev
	pcre2-dev xmlto perl-error"
# note that order matters
subpackages="$pkgname-dbg
	$pkgname-bash-completion
	perl-$pkgname-svn:_perl_git_svn:noarch
	perl-$pkgname:_perl_git:noarch
	$pkgname-svn::noarch
	$pkgname-email
	$pkgname-fast-import:_fast_import
	$pkgname-cvs::noarch
	$pkgname-daemon
	$pkgname-daemon-openrc:daemon_openrc
	$pkgname-gitweb::noarch
	$pkgname-subtree::noarch
	$pkgname-perl:_git_perl:noarch
	$pkgname-diff-highlight:diff_highlight:noarch
	"

source="https://www.kernel.org/pub/software/scm/git/git-$pkgver.tar.xz
	git-daemon.initd
	git-daemon.confd
	fix-t4219-with-sticky-bit.patch
	"

_gitcoredir=/usr/libexec/git-core

if [ -z "$BOOTSTRAP" ]; then
	makedepends="$makedepends asciidoc python3-dev tcl tk libsecret-dev glib-dev"
	subpackages="$subpackages
		$pkgname-doc
		$pkgname-p4::noarch
		$pkgname-gui::noarch
		$pkgname-gitk::noarch
		$pkgname-subtree-doc:subtree_doc:noarch
		$pkgname-credential-libsecret:credential_libsecret
		"
fi

prepare() {
	default_prepare

	cat >> config.mak <<-EOF
		NO_GETTEXT=YesPlease
		NO_SVN_TESTS=YesPlease
		NO_REGEX=YesPlease
		USE_LIBPCRE2=YesPlease
		NO_SYS_POLL_H=1
		CFLAGS=$CFLAGS
		ICONV_OMITS_BOM=Yes
		PYTHON_PATH=/usr/bin/python3
	EOF

	if [ -n "$BOOTSTRAP" ]; then
		cat >> config.mak <<-EOF
			NO_PYTHON=YesPlease
			NO_TCLTK=YesPlease
		EOF
	fi
}

build() {
	make prefix=/usr DESTDIR="$pkgdir" perllibdir=$(_perl_config vendorlib)

	if [ -z "$BOOTSTRAP" ]; then
		# generate manpages outside of package()
		make prefix=/usr man
		cd "$builddir"/contrib/credential/libsecret
		make prefix=/usr
	fi
}

check() {
	make prefix=/usr DESTDIR="$pkgdir" \
		perlibdir=$(_perl_config vendorlib) \
		test
}

package() {
	local install_man
	install_man=install-man
	if [ -n "$BOOTSTRAP" ]; then
		install_man=""
	fi

	make prefix=/usr DESTDIR="$pkgdir" INSTALLDIRS=vendor \
		perllibdir=$(_perl_config vendorlib) \
		install $install_man

	mkdir -p "$pkgdir"/var/git

	install -Dm755 "$srcdir"/git-daemon.initd "$pkgdir"/etc/init.d/git-daemon
	install -Dm644 "$srcdir"/git-daemon.confd "$pkgdir"/etc/conf.d/git-daemon

	install -Dm644 contrib/completion/git-completion.bash \
		"$pkgdir"/usr/share/bash-completion/completions/git

	if [ -z "$BOOTSTRAP" ]; then
		install -Dm755 contrib/credential/libsecret/git-credential-libsecret "$pkgdir"/usr/libexec/git-core
	fi
}

_perl_git_svn() {
	pkgdesc="Perl interface to Git::SVN"
	depends="git=$pkgver-r$pkgrel perl-git=$pkgver-r$pkgrel"
	local vendorlib

	vendorlib=$(_perl_config vendorlib)

	mkdir -p "$subpkgdir"/$vendorlib/Git
	mv "$pkgdir"/$vendorlib/Git/SVN* "$subpkgdir"/$vendorlib/Git/
}

_perl_git() {
	pkgdesc="Perl interface to Git"
	depends="git=$pkgver-r$pkgrel perl-error"

	local i
	for i in "$(_perl_config vendorlib)" "$(_perl_config vendorarch)"; do
		[ -d "$pkgdir/$i" ] || continue
		mkdir -p "$subpkgdir"/${i%/*}
		mv "$pkgdir"/$i "$subpkgdir"/$i
	done
}

email() {
	pkgdesc="Git tools for sending email"
	depends="perl perl-git=$pkgver-r$pkgrel perl-net-smtp-ssl perl-authen-sasl"

	mkdir -p "$subpkgdir"/$_gitcoredir
	mv "$pkgdir"/$_gitcoredir/*email* "$pkgdir"/$_gitcoredir/*imap* \
		"$subpkgdir"/$_gitcoredir
}

svn() {
	pkgdesc="Subversion support for git"
	depends="perl perl-git-svn=$pkgver-r$pkgrel perl-subversion
		perl-term-readkey"

	mkdir -p "$subpkgdir"/$_gitcoredir
	mv "$pkgdir"/$_gitcoredir/git-svn \
		"$subpkgdir"/$_gitcoredir/
}

cvs() {
	pkgdesc="Git tools for importing CVS repositories"
	depends="perl perl-git=$pkgver-r$pkgrel cvs perl-dbd-sqlite"

	mkdir -p "$subpkgdir"/usr/bin "$subpkgdir"/$_gitcoredir
	mv "$pkgdir"/usr/bin/git-cvs* "$subpkgdir"/usr/bin/
	mv "$pkgdir"/$_gitcoredir/*cvs* "$subpkgdir"/$_gitcoredir
}

_fast_import() {
	pkgdesc="Git backend for fast Git data importers"
	depends="git=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/$_gitcoredir
	mv "$pkgdir"/$_gitcoredir/git-fast-import "$subpkgdir"/$_gitcoredir/
}

p4() {
	pkgdesc="Git tools for working with Perforce depots"
	depends="git=$pkgver-r$pkgrel git-fast-import=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/$_gitcoredir/mergetools
	mv "$pkgdir"/$_gitcoredir/*p4* "$subpkgdir"/$_gitcoredir/
	mv "$pkgdir"/$_gitcoredir/mergetools/*p4* \
		"$subpkgdir"/$_gitcoredir/mergetools/
}

daemon() {
	pkgdesc="Git protocol daemon"
	depends="git=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/$_gitcoredir
	mv "$pkgdir"/$_gitcoredir/git-daemon \
		"$pkgdir"/$_gitcoredir/git-http-backend \
		"$pkgdir"/$_gitcoredir/git-shell \
		"$subpkgdir"/$_gitcoredir/
}

daemon_openrc() {
	pkgdesc="Git protocol daemon"

	default_openrc

	depends="git-daemon=$pkgver-r$pkgrel $depends_openrc"
	install_if="openrc $pkgname-daemon=$pkgver-r$pkgrel"
}

gitweb() {
	pkgdesc="Simple web interface to git repositories"
	depends="git=$pkgver-r$pkgrel perl"

	mkdir -p "$subpkgdir"/usr/share "$subpkgdir"$_gitcoredir
	mv "$pkgdir"/usr/share/gitweb "$subpkgdir"/usr/share/
	mv "$pkgdir"/$_gitcoredir/git-instaweb "$subpkgdir"$_gitcoredir/
}

subtree() {
	depends="git=$pkgver-r$pkgrel"
	pkgdesc="Split git repository into subtrees"

	cd "$builddir"/contrib/subtree
	make prefix=/usr DESTDIR="$pkgdir"
	make install prefix=/usr DESTDIR="$subpkgdir"
}

subtree_doc() {
	pkgdesc="Split git repository into subtrees (documentation)"
	depends=""
	install_if="docs git-subtree=$pkgver-r$pkgrel"

	cd "$builddir"/contrib/subtree
	make install-man prefix=/usr DESTDIR="$subpkgdir"
	gzip -9 "$subpkgdir"/usr/share/man/man1/git-subtree.1
}

gui() {
	pkgdesc="GUI interface for git"
	depends="git=$pkgver-r$pkgrel tcl tk"

	mkdir -p "$subpkgdir"/usr/share "$subpkgdir"/usr/libexec/git-core
	mv "$pkgdir"/usr/share/git-gui "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/libexec/git-core/git-citool \
		"$pkgdir"/usr/libexec/git-core/git-gui \
		"$pkgdir"/usr/libexec/git-core/git-gui--askpass \
		"$subpkgdir"/usr/libexec/git-core/
}

gitk() {
	pkgdesc="Gitk interface for git"
	depends="git=$pkgver-r$pkgrel tcl tk"

	mkdir -p "$subpkgdir"/usr/share "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/share/gitk "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/bin/gitk "$subpkgdir"/usr/bin/
}

diff_highlight() {
	pkgdesc="diff highlight for git"
	depends="git=$pkgver-r$pkgrel perl"

	cd "$builddir"/contrib/diff-highlight

	make prefix=/usr DESTDIR="$subpkgdir"

	mkdir -p "$subpkgdir"/usr/bin
	install -Dm755 diff-highlight "$subpkgdir"/usr/bin/
}

credential_libsecret() {
	pkgdesc="libsecret-based credential helper"
	depends="git=$pkgver-r$pkgrel"

	amove usr/libexec/git-core/git-credential-libsecret
}

# catch-the-rest of stuff that needs perl
_git_perl() {
	pkgdesc="Additional Git commands that requires perl"
	depends="git=$pkgver-r$pkgrel perl-git=$pkgver-r$pkgrel perl"
	install_if="git=$pkgver-r$pkgrel perl"

	mkdir -p "$subpkgdir"/usr/lib
	cd "$pkgdir"
	find -type f | xargs file --mime-type | grep perl | cut -d: -f1| while read f; do
		mkdir -p "$subpkgdir"/${f%/*}
		mv "$f" "$subpkgdir"/${f%/*}
	done
	find "$subpkgdir" -name perllocal.pod -delete
}

# Prints values of the Config.pm variable $1.
_perl_config() {
	perl -e "use Config; print \$Config{$1};"
}

sha512sums="
2ae3c845c9d0e0f5245e47f95c958c86a4aa2c47dfe31bff6fc81b2434d2e9402b7eced18700c04ba7158ed6a72807a81c4cde6a26dd30c969b4267b8fce4d0a  git-2.37.0.tar.xz
89528cdd14c51fd568aa61cf6c5eae08ea0844e59f9af9292da5fc6c268261f4166017d002d494400945e248df6b844e2f9f9cd2d9345d516983f5a110e4c42a  git-daemon.initd
fbf1f425206a76e2a8f82342537ed939ff7e623d644c086ca2ced5f69b36734695f9f80ebda1728f75a94d6cd2fcb71bf845b64239368caab418e4d368c141ec  git-daemon.confd
be5d568fc5b8b84c9afb97b31e471e41f32ccfe188eba0588ea0ef98b2d96c2ce4b2c1a3d70e88205aa4f6667f850b3f32c13bbb149ecddbf670344c162a4e25  fix-t4219-with-sticky-bit.patch
"
